<template>
    <div class="app-container">
        <!-- 表格-条件筛区域  -->
        <div class="filter-container">
            <el-input placeholder="请输入关键字" v-model="searchParam.SEARCH_LIKE_name" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"/>
            <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
            <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">新增</el-button>
        </div>

        <!-- 表格-内容区域 -->
        <el-table v-loading="listLoading" :key="tableKey" :data="tableList" border fit highlight-current-row style="width: 100%;">
            <el-table-column v-for="column in columns" :key="column.prop"
                             :prop="column.prop"
                             :label="column.label"
                             :width="column.width"
                             :align="column.align?column.align:'center'"
                             :min-width="column.minWidth"
                             :sortable="column.sortable"
                             :fixed="column.fixed"
                             :type="column.type" :formatter="column.formatter">
                <template slot-scope="scope">
                    <img v-if="column.imgFlag==true" :src="scope.row[column.prop]" width="60" height="60" class="img"/>
                    <!-- 行内显示开关按钮 -->
                    <el-switch v-else-if="column.switchFlag==true" v-model="scope.row[column.prop]" :active-value="1" :inactive-value="0"  disabled />
                    <span v-else-if="column.formatter"> {{column.formatter(scope.row)}}</span>
                    <span v-else>{{scope.row[column.prop]}}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                    <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 表格-页脚区域 -->
        <pagination v-show="tableTotal>0" :total="tableTotal" :page.sync="pageRequest.pageNum" :limit.sync="pageRequest.pageSize" @pagination="getList"/>

        <!--新增编辑界面-->
        <el-dialog :title="operation?'新增':'编辑'" width="45%" top="5vh" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
            <div style="height:550px;overflow-y: scroll;">
                <el-form :model="dataForm" label-width="140px" :rules="dataFormRules" ref="dataForm" style="width: 80%">

                    <#if vo.columns?exists>
                        <#list vo.columns as formField>
                            <#if formField.columnShowFlag == false>
                            <#elseif formField.columnType == "0">
                    <el-form-item label="${formField.columnRemark}" prop="${formField.columnName}">
                        <el-input v-model="dataForm.${formField.columnName}" auto-complete="off"/>
                    </el-form-item>
                            <#elseif formField.columnType == "1">
                    <el-form-item label="${formField.columnRemark}" prop="${formField.columnName}">
                        <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.${formField.columnName}"
                                        value-format="yyyy-MM-dd"
                                        style="width: 100%;"></el-date-picker>
                    </el-form-item>
                            <#elseif formField.columnType == "2">
                    <el-form-item label="${formField.columnRemark}" prop="${formField.columnName}">
                        <el-select v-model="dataForm.${formField.columnName}" placeholder="请选择活动区域">
                            <el-option label="区域一" value="1"></el-option>
                            <el-option label="区域二" value="2"></el-option>
                        </el-select>
                    </el-form-item>
                            <#elseif formField.columnType == "3">
                    <el-form-item label="${formField.columnRemark}" prop="${formField.columnName}">
                      <!--此处可引入上传组件-->
                    </el-form-item>
                            <#elseif formField.columnType == "4">
                    <el-form-item label="${formField.columnRemark}" prop="${formField.columnName}">
                        <el-input type="textarea" v-model="dataForm.${formField.columnName}" ></el-input>
                    </el-form-item>
                            <#elseif formField.columnType == "5">
                    <el-form-item label="${formField.columnRemark}" prop="${formField.columnName}">
                        <el-switch v-model="dataForm.${formField.columnName}" :active-value="1"
                                   :inactive-value="0"></el-switch>
                    </el-form-item>
                            <#elseif formField.columnType == "6">
                    <el-form-item label="${formField.columnRemark}" prop="${formField.columnName}">
                        <el-checkbox-group v-model="dataForm.${formField.columnName}">
                            <el-checkbox label="1" >多选1</el-checkbox>
                            <el-checkbox label="2" >多选2</el-checkbox>
                            <el-checkbox label="3" >多选3</el-checkbox>
                            <el-checkbox label="4" >多选4</el-checkbox>
                        </el-checkbox-group>
                    </el-form-item>
                            <#elseif formField.columnType == "7">
                    <el-form-item label="${formField.columnRemark}" prop="${formField.columnName}">
                        <el-radio-group v-model="dataForm.${formField.columnName}">
                            <el-radio label="0" >单选1</el-radio>
                            <el-radio label="1" >单选2</el-radio>
                        </el-radio-group>
                    </el-form-item>
                            <#else>
                                未指定
                            </#if>
                        </#list>
                    </#if>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button size="mini" @click.native="closeDialog()">取消</el-button>
                <el-button size="mini" @click.native="saveFormData()" :loading="editLoading" type="primary">提交
                </el-button>
            </div>
        </el-dialog>


    </div>
</template>
<script>
    import baseData from '@/mixin/base-data'
    // import {} from '@/api/modules/123'
    export default {
        name: 'product-brand',
        mixins: [baseData],
        components: {  },
        directives: {  },
        filters: { },
        data() {
            return {
                tableKey: 0,
                tableList: [], // 表格数据源
                tableTotal: 0, // 表格总条数数据
                listLoading: true,
                pageRequest: { pageNum: 1, pageSize: 10 },
                searchParam:{ SEARCH_LIKE_name:''},
                dialogFormVisible: false,//默认不显示弹出框
                operation: true, // true:新增, false:编辑
                editLoading: false,//保存表单时loading框
                columns: [
                    <#if vo.columns?exists>
                        <#list vo.columns as item>
                    {prop: '${item.columnName}',  label: '${item.columnRemark}',  sortable:false  },
                        </#list>
                    </#if>
                ],
                fileList: [],
                // 新增编辑界面数据
                dataForm: {
                <#if vo.columns?exists>
                    <#list vo.columns as item>
                        ${item.columnName}: '' ,
                    </#list>
                </#if>
          },
                //表单检验规则
                dataFormRules: {
            <#if vo.columns?exists>
                <#list vo.columns as item>
                    <#if item.columnMustFlag==true && item.columnHiddenFlag==true>
                        ${item.columnName}: [ { required: true, message: '请输入${item.columnRemark}', trigger: 'blur' } ],
                    </#if>
                </#list>
            </#if>
        },
        }
        },
        created() {
            this.getList();
        },
        methods: {
            async getList() {
              this.listLoading = true
              // 【API请求】----获取用户列表数据
              const res = await get${vo.describe}List(this.pageRequest)
              try {
                if (res.code === 0) {
                  const { content, total } = res.data
                  this.tableList = content
                  this.tableTotal = total
                }
                this.listLoading = false
              }catch (e) {
                this.listLoading = false
                throw e
              }
            },
            //获取表格数据
            //表格数据筛选
            handleFilter() {
                this.pageRequest.searchParam = this.searchParam
                this.getList()
            },
            //【按钮点击】--- 弹出新增表单
            handleCreate() {
                this.operation = true;
                this.initDataForm();;
                this.dialogFormVisible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].clearValidate()
                this.editLoading = false;
            })
            },
            //【按钮点击】--- 弹出编辑表单
            handleUpdate(row) {
                this.operation = false;
                this.dataForm = Object.assign({}, row)
                //显示编辑菜单
                this.dialogFormVisible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].clearValidate()
                this.editLoading = false;
            })

            },
            //保存表单数据
           saveFormData() {
                this.$refs['dataForm'].validate(async(valid) => {
                    if (!valid) {
                    return false;
                }
                this.editLoading = true;
                //【API请求】----保存数据
                const res = await save${vo.describe}Info(this.dataForm)
                  try {
                    if (res.code===0){
                      this.dialogFormVisible = false;//关闭表单页面
                      this.initDataForm();//重置表单数据
                      this.getList();//重新获取表格数据
                      this.$notify({ title: '成功',  message: '保存成功', type: 'success', duration: 2000 })
                    }
                    this.editLoading = false;//关闭loading框
                  }catch (e) {
                    throw e
                  }

              })
            },
            //【按钮点击】---删除行
            handleDelete(row) {
              this.$confirm('确认删除该记录吗？', '提示', {}).then(async() => {
                  //【API请求】---- 删除数据
                const { id } = row
                const res = await delete${vo.describe}Info({ids:id})
                try {
                  if (res.code===0){
                    this.getList();
                    this.$message({  message: '操作成功', type: 'success' })
                  }
                }catch (e) {
                  throw e
                }
              });
            },
            //初始化表单数据，重新渲染
            initDataForm(){
              Object.assign(this.$data.dataForm, this.$options.data().dataForm)
            },
            //关闭表单弹出框
            closeDialog(){
                this.$refs['dataForm'].resetFields();
                this.dialogFormVisible = false
            },
        }
    }
</script>
